import React, {Component, PropTypes} from 'react'

export default class TriggerGetData extends Component {
    shouldComponentUpdate(nextProps, nextState) {
        const options = this.props.options;
        const trigger = options.trigger;
        var shouldTrigger = false;

        if(trigger && trigger.length) {
            trigger.forEach((i) => {
                if(JSON.stringify(this.props[i]) !== JSON.stringify(nextProps[i])){
                    shouldTrigger = true
                }
            });

            if(shouldTrigger) {
                setTimeout(() => this.sendRequst())
            }
        }

        return true
    }
    componentDidMount() {
        this.sendRequst()
    }
    sendRequst() {
        const { options, isPending, getData } = this.props;
        const trigger = options.trigger;
        var shouldSend = true;

        if(getData){
            let query = {};

            if(trigger && trigger.length){
                trigger.forEach((i) => {
                    if(i != 'trigger'){
                        if(typeof this.props[i] == 'undefined'){
                            shouldSend = false
                        }
                        query[i] = this.props[i]
                    }
                })
            }

            //query = merge(query, params)

            if(shouldSend){
                getData(query)
            }
        }
    }
}

TriggerGetData.defaultProps = {
    options: {
        trigger: ['platId']
    }
}